//organization_invite
//邀请回答—机构
exports.organization_invite = function (req, res) {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var ficid = 0;

    var inviteCount = 0;
    //任务流创建
    async.waterfall([

            //1.判断这个问题是否被邀请过
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchForumInviteContent + "/fic_invited_type/0/fic_cid/" + restfulParams.fc_id,
                        method: 'POST'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        if (data.data.length > 0) {

                            ficid = data.data[0].fic_id;

                            cb();

                            //新增问题记录
                        } else {

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.addForumInviteContent,
                                    method: 'POST',
                                    data: {
                                        fic_cid: restfulParams.fc_id,
                                        fic_buid: restfulParams.f_id
                                    }
                                }
                                ,
                                function (err, data) {

                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, "邀请回答失败");
                                    }

                                    ficid = data;

                                    cb();

                                }
                            );

                        }

                    }
                );

            },

            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchForumInviteLog + "/fil_uid/" + restfulParams.u_id + "/fil_ficid/" + ficid+"/fil_fid/"+restfulParams.f_id,
                        method: 'GET'
                    }
                    ,
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "邀请回答失败");
                        }

                        if (data.data.length > 0) {
                            return RES.response(res, false, "您已邀请过该机构或达人回答了");
                        }

                        cb();

                    }
                );


            },

            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.addForumInviteLog,
                        method: 'POST',
                        data: {
                            fil_uid: restfulParams.u_id,
                            fil_ficid: ficid,
                            fil_fcid: restfulParams.fc_id,
                            fil_fid: restfulParams.f_id,
                        }
                    }
                    ,
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "邀请回答失败");
                        }

                        cb();

                    }
                );
            },
            //查询机构被邀请回答的次数
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + restfulParams.f_id,
                        method: 'POST',
                        data: {
                            fields: ["o_id", "o_invite_count"].join(",")
                        }
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "邀请回答失败");
                        }
                        inviteCount = data.data;

                        //console.log(inviteCount);
                        // resultData.inviteData = inviteCount;
                        cb();

                    }
                );
            },
            //更新机构被邀请回答的次数
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.updateOrganization,
                        method: 'POST',
                        data: {
                            o_id: restfulParams.f_id,
                            o_invite_count: parseInt(inviteCount[0].o_invite_count) + 1
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }

                        resultData.updateData = data;
                        cb();
                    }
                );

            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true);

        }
    );

}